package br.com.itc.componentes.containers
{
	
	import br.com.itc.componentes.validacao.IValidavel;
	import br.com.itc.componentes.validacao.RealizarValidacao;
	
	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	
	public class CanvasITC extends Canvas
	{
		public function CanvasITC()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init);
		}
		
		private function init(event: FlexEvent): void {

		}
		
		private var _objetoValidavel: RealizarValidacao;
		
		public function realizarValidacao(): RealizarValidacao {
			_objetoValidavel = new RealizarValidacao;
			efetuaValidacao(getChildren());
			return _objetoValidavel;
		}
		
		private function efetuaValidacao(colecao: Array): void {
			var componentePrincipal: Object;
			for each(componentePrincipal in colecao) {
				if (componentePrincipal is IValidavel) {
					var componenteValidavel: IValidavel = componentePrincipal as IValidavel;
					if (!componenteValidavel.isValido()) {
						_objetoValidavel.valido = false;
						_objetoValidavel.campoFoco = componenteValidavel;
						return;
					}
				}
				if (componentePrincipal.hasOwnProperty("getChildren") 
						&& componentePrincipal.getChildren() != null
						&& componentePrincipal.getChildren().length > 0) {
							
					efetuaValidacao(componentePrincipal.getChildren());
				}
			}
		}
	}
}